<template>
  <!-- 放置一个图标 -->
  <div>
    <svg-icon
      class="fullscreen"
      @click="toggleScreen"
      :icon-class="isScreenFull ? 'exit-fullscreen' : 'fullscreen'"
    />
  </div>
</template>

<script>
export default {
  name: 'ScreenFull',
  data () {
    return {
      isScreenFull: false
    }
  },
  methods: {
    toggleScreen () {
      if (this.isScreenFull) {
        document.exitFullscreen()
      } else {
        document.documentElement.requestFullscreen()
      }
      this.isScreenFull = !this.isScreenFull
    }
  },
  mounted () {
    document.addEventListener('fullscreenchange', e => {
      // 监听到屏幕变化，在回调中判断是否已退出全屏 如果已退出全屏 把本地状态修改为false
      let isFull = document.fullscreenElement
      if (!isFull) {
        this.isScreenFull = false
      }
    })
  }
}
</script>

<style lang="scss" scoped>
.fullscreen {
  width: 20px;
  height: 20px;
  color: #fff;
  cursor: pointer;
}
</style>

1 开启全屏: document.documentElement.requestFullscreen()

2 关闭全屏: document.exitFullscreen()